/************************************************************************
 * File         :   cppTutorSTLMap.cpp
 * Description  :   std :: map class definition 
 * Author       :   eLearning Business Unit
 * Company      :   ccplusplus.com
 * Date         :   created     11:00 15-06-2013
 *              :   revision    
 * Version      :   1.0.0
 * Notes        :
 *
 ************************************************************************/
#include "cppTutorSTLMap.h"

// log framework
#include "cppTutorLog.h"
extern CcppTutorLog Log;

using namespace std;

CcppTutorSTLMap :: CcppTutorSTLMap () : m_bReturnStatus (true), m_nTemp(1), m_nKey (1) {
    Log << "ENTER : CcppTutorSTLMap :: CcppTutorSTLMap()" << endl;
    Log << "EXIT  : CcppTutorSTLMap :: CcppTutorSTLMap()" << endl;
}

CcppTutorSTLMap :: ~CcppTutorSTLMap () {
    Log << "ENTER : CcppTutorSTLMap :: ~CcppTutorSTLMap()" << endl;
    Log << "EXIT  : CcppTutorSTLMap :: ~CcppTutorSTLMap()" << endl;
}

bool CcppTutorSTLMap :: m_mapDemo () {
    Log << "ENTER : CcppTutorSTLMap :: m_mapDemo()" << endl;

#ifdef STL_MAP_CREATE    
    m_CreateWeekDays ();
#endif //STL_MAP_CREATE
    
#ifdef STL_MAP_DISPLAY
    m_DisplayWeekDays ();
#endif //STL_MAP_DISPLAY

#ifdef STL_MAP_ERASE
    m_eraseWeekdays ();
    m_DisplayWeekDays ();
#endif //STL_MAP_ERASE

#ifdef STL_MAP_INSERT
    m_insertWeekdays ();
    m_DisplayWeekDays ();
#endif //STL_MAP_INSERT

#ifdef STL_MAP_COUNT
    m_countWeekdays ();
#endif //STL_MAP_COUNT
    
#ifdef STL_MAP_OPERATOR_EQUAL
    m_operatorEqualWeekdays ();
#endif //STL_MAP_OPERATOR_EQUAL

    Log << "EXIT  : CcppTutorSTLMap :: m_mapDemo() [" << m_bReturnStatus << " : " << __LINE__ << "]"<< endl;
    return m_bReturnStatus;
}

#ifdef  STL_MAP_CREATE
bool CcppTutorSTLMap :: m_CreateWeekDays () {
    Log << "ENTER : CcppTutorSTLMap :: m_CreateWeekDays()" << endl;
    
    m_bReturnStatus = true;

    weekdays [1] = "Monday";
    weekdays [2] = "Tuesday";
    weekdays [3] = "Wednesday";
    weekdays [4] = "Thursday";
    weekdays [5] = "Friday";
    weekdays [6] = "Saturday";
    weekdays [7] = "Sunday";
    
    Log << "EXIT  : CcppTutorSTLMap :: m_CreateWeekDays() [" << m_bReturnStatus << " : " << __LINE__ << "]"<< endl;
    return m_bReturnStatus;
}
#endif //STL_MAP_CREATE

#ifdef STL_MAP_DISPLAY
bool CcppTutorSTLMap :: m_DisplayWeekDays () {
    Log << "ENTER : CcppTutorSTLMap :: m_DisplayWeekDays()" << endl;
    int nI;
    m_bReturnStatus = true;
    
    cout << "A map of weekdays created with size [" << weekdays.size() << "]" << endl;
    
    cout << "\nStandard Template library : map\n" << endl;
    cout << "\t-----------------------------------" << endl;
    cout << "\t S.No.                    Weekdays " << endl;
    cout << "\t-----------------------------------" << endl;
    nI = 1;
    // for (weekdays_it = weekdays.begin (); weekdays_it !=weekdays.end (); ++weekdays_it) {
        // cout << "\t [" << nI << "] \t\t\t " << weekdays [nI] << endl;
        // ++ nI;
    // }
    // show content:
    for (weekdays_it=weekdays.begin(); weekdays_it!=weekdays.end(); ++weekdays_it) {
        cout << "\t [" << weekdays_it->first << "] \t\t\t " << weekdays_it->second << endl;
    }
    cout << "\t-----------------------------------" << endl;
    
    Log << "EXIT  : CcppTutorSTLMap :: m_DisplayWeekDays() [" << m_bReturnStatus << " : " << __LINE__ << "]"<< endl;
    return m_bReturnStatus;
}
#endif //STL_MAP_DISPLAY

#ifdef STL_MAP_ERASE
bool CcppTutorSTLMap :: m_eraseWeekdays () {
    Log << "ENTER : CcppTutorSTLMap :: m_eraseWeekdays()" << endl;
    
    m_bReturnStatus = true;
    cout << "Enter the S.No of the weekday you want to erase, " << endl;
    cin  >> m_nTemp;
    
    // erase by ieretator method
    // weekdays_it = weekdays.find (m_nTemp);
    // weekdays.erase (weekdays_it);

    // erasing by key
    weekdays.erase (m_nTemp);

    m_DisplayWeekDays ();
    
    Log << "EXIT  : CcppTutorSTLMap :: m_eraseWeekdays() [" << m_bReturnStatus << " : " << __LINE__ << "]"<< endl;
    return m_bReturnStatus;    
}
#endif //STL_MAP_ERASE

#ifdef STL_MAP_INSERT
bool CcppTutorSTLMap :: m_insertWeekdays () {
    Log << "ENTER : CcppTutorSTLMap :: m_insertWeekdays()" << endl;
    
    m_bReturnStatus = true;
    cout << "Inser the key and value to insert in the weekdays" << endl;
    cout << "Key : ";
    cin >> m_nKey;
    cout << "Value : ";
    cin >> m_strWeekDay;
    cout << endl;
    
    //insert function version (with hint position)
    // weekdays_it = weekdays.begin ();
    // weekdays.insert (weekdays_it, pair <int, string> (m_nKey, m_strWeekDay));

    pair <map <int, std :: string> :: iterator, bool> ret;
    ret = weekdays.insert (pair <int, string>(m_nKey, m_strWeekDay));
    if (ret.second == false) {
        cout << "Duplicate key found !!!" << endl;
        Log << "EXIT  : CcppTutorSTLMap :: m_insertWeekdays() [" << m_bReturnStatus << " : " << __LINE__ << "]"<< endl;
        return m_bReturnStatus;    
    }
    
    Log << "EXIT  : CcppTutorSTLMap :: m_insertWeekdays() [" << m_bReturnStatus << " : " << __LINE__ << "]"<< endl;
    return m_bReturnStatus;    
}
#endif //STL_MAP_INSERT

#ifdef STL_MAP_COUNT
bool CcppTutorSTLMap :: m_countWeekdays () {
    Log << "ENTER : CcppTutorSTLMap :: m_countWeekdays()" << endl;
    
    m_bReturnStatus = true;

    unsigned int nKey = 0, nSearch = 0;
    cout << "Enter the key to search for " << endl;
    cin  >> nSearch;
    for (nKey; nKey <= weekdays.size(); ++nKey) {
        if (weekdays.count (nSearch)) {
            cout << "Key [" << nSearch << "] found !!!" << endl;
        }
        else {
            cout << "Key [" << nSearch << "] not found !!!" << endl;
            m_bReturnStatus = false;
        }
    }
    
    Log << "EXIT  : CcppTutorSTLMap :: m_countWeekdays() [" << m_bReturnStatus << " : " << __LINE__ << "]"<< endl;
    return m_bReturnStatus;    
}
#endif //STL_MAP_COUNT

#ifdef STL_MAP_OPERATOR_EQUAL
bool CcppTutorSTLMap :: m_operatorEqualWeekdays () {
    Log << "ENTER : CcppTutorSTLMap :: m_operatorEqualWeekdays()" << endl;
    
    m_bReturnStatus = true;
     
    cout << "weekdays contains : " << endl;
    for (weekdays_it=weekdays.begin(); weekdays_it!=weekdays.end(); ++weekdays_it) {
        cout << "\t [" << weekdays_it->first << "] \t\t\t " << weekdays_it->second << endl;
    }
    cout << "copying the weekdays to weekdays_copy" << endl;
    weekdays_copy = weekdays;
    cout << "weekdays_copy contains : " << endl;
    for (weekdays_copy_it=weekdays_copy.begin(); weekdays_copy_it!=weekdays_copy.end(); ++weekdays_copy_it) {
        cout << "\t [" << weekdays_copy_it->first << "] \t\t\t " << weekdays_copy_it->second << endl;
    }
    
    Log << "EXIT  : CcppTutorSTLMap :: m_operatorEqualWeekdays() [" << m_bReturnStatus << " : " << __LINE__ << "]"<< endl;
    return m_bReturnStatus;    
}
#endif //STL_MAP_OPERATOR_EQUAL

#ifdef STL_MAP_SWAP
bool CcppTutorSTLMap :: m_swapWeekdays () {
    Log << "ENTER : CcppTutorSTLMap :: m_swapWeekdays()" << endl;
    
    m_bReturnStatus = true;

    
    
    Log << "EXIT  : CcppTutorSTLMap :: m_swapWeekdays() [" << m_bReturnStatus << " : " << __LINE__ << "]"<< endl;
    return m_bReturnStatus;    
}
#endif //STL_MAP_SWAP
